package com.wmx.mybatisstudy.controller;

import com.wmx.mybatisstudy.MybatisStudyApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangMaoXiong
 * @version 1.0
 * @date 2020/10/19 19:36
 */
@RestController
public class AppController {

    private static Logger logger = LoggerFactory.getLogger(AppController.class);

    /**
     * http://127.0.0.1:8080/app/shutdown
     *
     * @return
     */
    @GetMapping("app/shutdown")
    public void shutdown() {
        logger.info("用户手动请求关闭服务.");
        //关闭此应用程序上下文，释放实现可能持有的所有资源和锁，包括销毁所有缓存的单例bean
        MybatisStudyApplication.configurableApplicationContext.close();
    }

    /**
     * http://127.0.0.1:8080/app/setSystemProperty?key=token&value=898989TT6767AA
     * 设置的 value 值相当于一个静态的系统全局变量,存储在内存中,任何用户都可以在应用的任何地方通过System.getProperty(key)获取
     *
     * @param key
     * @param value
     * @return
     */
    @GetMapping("app/setSystemProperty")
    public String setSystemProperty(@RequestParam String key, @RequestParam String value) {
        logger.info("key={},value={}", key, value);
        System.setProperty(key, value);
        return "Success";
    }

    /**
     * http://127.0.0.1:8080/app/getSystemProperty?key=token
     *
     * @param key
     * @return
     */
    @GetMapping("app/getSystemProperty")
    public String getSystemProperty(@RequestParam String key) {
        logger.info("key={}", key);
        String property = System.getProperty(key);
        return property;
    }
}
